﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace AMSReportBuildService
{
    //资产异动报表生成XML文件类
    class AssetChangeXmlFile
    {
        /// <summary>
        /// 获取每年的总数
        /// </summary>
        /// <returns></returns>
        private DataSet CountOfYear(string strYear)
        {
            string sqlString = @"Select * from (Select convert(char(4),wo.[CreateDate],112) as Year,count(*) as Amount from dbo.AmsWorkBill_WoInfo wo 
join dbo.AmsWorkBill_WoCommand cmd on wo.Code = cmd.WoCode
				Group by convert(char(4),wo.[CreateDate],112) ) a ";
            if (strYear.Length != 0)
                sqlString += " Where Year='" + strYear + "'";
            return DataAccess.GetData(sqlString);
        }

        /// <summary>
        /// 获取每个异动类型总数
        /// </summary>
        /// <param name="strYear"></param>
        /// <returns></returns>
        private DataSet CountOfTypeName(string strYear)
        {
            string sqlString = @"Select * from (Select dbo.uf_GetWoilBillTypeName(wo.TypeName) TypeName, convert(char(4),wo.[CreateDate],112) as Year,count(*) as Amount from dbo.AmsWorkBill_WoInfo wo 
join dbo.AmsWorkBill_WoCommand cmd on wo.Code = cmd.WoCode
				Group by convert(char(4),wo.[CreateDate],112),dbo.uf_GetWoilBillTypeName(wo.TypeName)) a ";
            if (strYear.Length != 0)
                sqlString += " Where Year='" + strYear + "'";
            sqlString += @" Order by Year";
            return DataAccess.GetData(sqlString);
        }

        private string[] TypeName
        {
            get
            {
                string[] typename = new string[11];
                typename[0] = "领用";
                typename[1] = "维护";
                typename[2] = "借用";
                typename[3] = "返还";
                typename[4] = "退仓";
                typename[5] = "调拨";
                typename[6] = "报废";
                typename[7] = "损毁";
                typename[8] = "减值";
                typename[9] = "启用";
                typename[10] = "停用";
                 return typename;
            }
        }

        private void CreateGraphXmlFile(string path, bool isAsset, bool isGraph)
        {
            var myXml = new XmlDocument();

            //建立Xml的定义声明
            XmlDeclaration dec = myXml.CreateXmlDeclaration("1.0", "GB2312", null);
            myXml.AppendChild(dec);

            XmlElement rootNode = myXml.CreateElement("chart");
            rootNode.SetAttribute("caption", "资产异动报表");
            rootNode.SetAttribute("xAxisName", "年份");
            rootNode.SetAttribute("yAxisName", "数量");
            rootNode.SetAttribute("baseFontSize", "12");
            rootNode.SetAttribute("palette", "1");
            myXml.AppendChild(rootNode);

            if (isGraph)
            {
                XmlElement categoriesNode = myXml.CreateElement("categories");
                myXml.DocumentElement.AppendChild(categoriesNode);
                foreach (string typeName in TypeName)
                {
                    XmlElement categoryNode = myXml.CreateElement("category");
                    categoryNode.SetAttribute("label", typeName);
                    categoriesNode.AppendChild(categoryNode);
                }
            }
            else
            {
                XmlElement dataNode = myXml.CreateElement("data");
                dataNode.SetAttribute("name", "年份");
                myXml.DocumentElement.AppendChild(dataNode);
                foreach (string typeName in TypeName)
                {
                    XmlElement monthNode = myXml.CreateElement("TypeName");
                    monthNode.SetAttribute("amount", typeName);
                    dataNode.AppendChild(monthNode);
                }
            }

            DataSet YearList = CountOfYear("");

            foreach (DataRow yearRow in YearList.Tables[0].Rows)
            {
                var i = 0;
                var j = 0;
                XmlElement dataNode = myXml.CreateElement("dataset");
                dataNode.SetAttribute("seriesName", yearRow["Year"].ToString());
                if (!isGraph)
                    dataNode.SetAttribute("amount", yearRow["Amount"].ToString());
                myXml.DocumentElement.AppendChild(dataNode);

                DataSet typeNameList = CountOfTypeName(yearRow[0].ToString());
                var rowCount = typeNameList.Tables[0].Rows.Count;

                foreach (DataRow typeNameRow in typeNameList.Tables[0].Rows)
                {
                    j++;
                    foreach (string currentTypeName in TypeName)
                    {

                        var typeNameNum = CaseStringToInt(currentTypeName);
                        if (Convert.ToInt32(typeNameNum) > i)
                        {


                            XmlElement setNode = myXml.CreateElement("set");
                            if (typeNameRow["TypeName"].ToString() != currentTypeName)
                            {
                                setNode.SetAttribute("name", currentTypeName);
                                setNode.SetAttribute("value", "0");
                                setNode.SetAttribute("percent", "0.00%");
                                dataNode.AppendChild(setNode);
                            }
                            else
                            {

                                setNode.SetAttribute("name", typeNameRow["TypeName"].ToString());
                                setNode.SetAttribute("value", typeNameRow["Amount"].ToString());
                                string toolText;
                                if (isGraph)
                                {
                                    toolText = typeNameRow["Amount"] + "，" + (Convert.ToDouble(typeNameRow["Amount"].ToString()) / Convert.ToInt32(yearRow["Amount"].ToString())).ToString("0.00%");
                                    setNode.SetAttribute("tooltext", toolText);
                                }
                                else
                                {
                                    toolText = (Convert.ToDouble(typeNameRow["Amount"].ToString()) / Convert.ToInt32(yearRow["Amount"].ToString())).ToString("0.00%");
                                    setNode.SetAttribute("percent", toolText);
                                }
                                dataNode.AppendChild(setNode);
                                if (j < rowCount)
                                    break;
                            }

                        }
                    }
                    i = Convert.ToInt32(CaseStringToInt(typeNameRow["TypeName"].ToString()));
                }

            }
            if (File.Exists(path))
                File.Delete(path);
            myXml.Save(path);
        }

        private string CaseStringToInt(string typename)
        {
            string currentTypeName;
            switch (typename)
            {
                case "领用":
                    currentTypeName = "01";
                    break;
                case "维护":
                    currentTypeName = "02";
                    break;
                case "借用":
                    currentTypeName = "03";
                    break;
                case "返还":
                    currentTypeName = "04";
                    break;
                case "退仓":
                    currentTypeName = "05";
                    break;
                case "调拨":
                    currentTypeName = "06";
                    break;
                case "报废":
                    currentTypeName = "07";
                    break;
                case "损毁":
                    currentTypeName = "08";
                    break;
                case "减值":
                    currentTypeName = "09";
                    break;
                case "启用":
                    currentTypeName = "10";
                    break;
                case "停用":
                    currentTypeName = "11";
                    break;
                  default:
                    currentTypeName = "01";
                    break;
            }
            return currentTypeName;
        }

        /// <summary>
        /// 创建GrowthTrendOfAsset.xml
        /// </summary>
        /// <param name="path"></param>
        public void CreateAssetGraphXmlFile(string path)
        {
            CreateGraphXmlFile(path, true, true);
        }

        ///// <summary>
        ///// 创建GrowthTrendOfEquipment.xml
        ///// </summary>
        ///// <param name="path"></param>
        //public void CreateEquipmentGraphXmlFile(string path)
        //{
        //    CreateGraphXmlFile(path, false, true);
        //}

        /// <summary>
        /// 创建GrowthTrendOfAssettb.xml
        /// </summary>
        /// <param name="path"></param>
        public void CreateAssetListXmlFile(string path)
        {
            CreateGraphXmlFile(path, true, false);
        }

        ///// <summary>
        ///// 创建GrowthTrendOfEquipmenttb.xml
        ///// </summary>
        ///// <param name="path"></param>
        //public void CreateEquipmentListXmlFile(string path)
        //{
        //    CreateGraphXmlFile(path, false, false);
        //}
    }
}
